<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<style type="text/css">
.ddl2pageButton{
	padding-left:5px;
	padding-top:5px;
}
.ddl2pageContent{
	padding-left:5px;
}
</style>

<script type="text/javascript">
var ddl2pageStep = 1;
var ddl2pageFrom3 = false;

$(function(){
	$('#ddl2page_prev').hide();
	$('#ddl2page_step2').css('display', 'none');
	$('#ddl2page_step3').css('display', 'none');
});

function ddl2Page(step){
	if(step == 2){
		if(!$.trim($('#ddl4page').val())){
			showErrorMessage('请输入ddl语句');
			ddl2pageStep--;
			return;
		}

		$('#ddl2page_step1').css('display', 'none');
		$('#ddl2page_step2').css('display', 'block');
		$('#ddl2page_step3').css('display', 'none');
		
		$('#ddl2page_step2').show();
		if(!ddl2pageFrom3){
			$('#ddl2page_step2').load('page/ddl2page_step2.dox', {'ddl':$('#ddl4page').val()});
		}
		
		$('#ddl2page_prev').show();
		$('#ddl2page_next').show();
		$('#ddl2page_clear').hide();
		
		$('#ddl2page_next').val('生成');
		
	}else if(step == 3){
		var checked = false;
		var keyword = false;
		
		$('#ddl2page_step1').css('display', 'none');
		$('#ddl2page_step2').css('display', 'none');
		$('#ddl2page_step3').css('display', 'block');

		$('#ddl2page_form').children().find('input[id$="page"]').each(function(){
			checked = checked || $(this).is(':checked');
			if($(this).attr('name') == 'PAGE' || $(this).attr('name') == 'ROWS' || $(this).attr('name') == 'ORDERBY'){
				keyword = true;
			}
		});
		//debugger;
		if(keyword){
			ddl2pageStep--;
			showErrorMessage('列名不能为[PAGE,ROWS,ORDERBY]');
			return;
		}
		
		if(checked){
			// save code config snapshot
			var config = {}
			config.dom = $('#ddl2page_pane').html();
			config.loginUser = '${loginName}';
			config.type = 3;
			config.table = $('#ddl2page_table').val();
			
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: 'code/snapshot.json',
				data: config,
				success: function(data){
					if(data && data.code != 0){
						handleError(data.code);
					}
				},
				error: function(data){
					
				}
			});
			
			// submit form to generate code
			$.ajax({
				type: "post",
				dataType: "html",
				url: 'page/ddl2page_step3.dox',
				data: $('#ddl2page_form').serialize(),
				success: function(data){
					ddl2pageFrom3 = true;
					
					$('#ddl2page_next').val('返回');
					
					$('#ddl2page_step3').html(data);
				},
				error: function(data){
					$('#ddl2page_step1').css('display', 'none');
					$('#ddl2page_step2').css('display', 'block');
					$('#ddl2page_step3').css('display', 'none');
					
					ddl2pageStep--;
					showErrorMessage('生成失败');
				}
			});
		}else{
			ddl2pageStep--;
			showErrorMessage('请至少选择一个字段');
		}
	}else{
		$('#ddl2page_step2').empty();
		$('#ddl2page_step2').append('<img alt="" src="resources/images/loading.gif">');
		
		$('#ddl2page_step3').empty();
		$('#ddl2page_step3').append('<img alt="" src="resources/images/loading.gif">');
		
		$('#ddl2page_step1').css('display', 'block');
		$('#ddl2page_step2').css('display', 'none');
		$('#ddl2page_step3').css('display', 'none');
		
		$('#ddl2page_prev').hide();
		$('#ddl2page_next').show();
		$('#ddl2page_clear').show();
		
		$('#ddl2page_next').val('下一步');
		
		ddl2pageFrom3 = false;
		ddl2pageStep = 1;
	}
}
</script>
<h1>根据建表语句(DDL)生成管理画面</h1>

<div id="ddl2page_step1" class="ddl2pageContent">
<h2>步骤1：输入DDL</h2>
<div>点击&nbsp;<a target="_blank" href="ddl_demo.html">这里</a>&nbsp;查看ddl示例</div>
<br/>
<textarea id="ddl4page" style="width:90%;height:500px"></textarea><br/>
</div>

<div id="ddl2page_step2">
<img alt="请稍候..." src="resources/images/loading.gif">
</div>

<div id="ddl2page_step3">
<img alt="请稍候..." src="resources/images/loading.gif">
</div>

<div class="ddl2pageButton">
<input type="button" id="ddl2page_prev" value="上一步" onclick="ddl2Page(--ddl2pageStep);"/>
<input type="button" id="ddl2page_next" value="下一步" onclick="ddl2Page(++ddl2pageStep);"/>
<input type="button" id="ddl2page_clear" value="清空" onclick="clearInput('#ddl4page');"/>
</div>
